package mysql

import (
	"video-hub/videorpc/videorpc"

	"gorm.io/gorm"
)

type Video struct {
	gorm.Model
	UserId        int64  `gorm:"type:int(11);not null;comment:用户id"`
	VideoTitle    string `gorm:"type:varchar(255);not null;comment:视频标题"`
	VideoCategory string `gorm:"type:varchar(255);not null;comment:视频分类"`
	VideoLink     string `gorm:"type:varchar(255);not null;comment:视频URL"`
	CoverLink     string `gorm:"type:varchar(255);not null;comment:视频封面"`
	VideoIntro    string `gorm:"type:varchar(255);not null;comment:视频简介"`
	PlayNum       int64  `gorm:"type:int(11);default:0;not null;comment: 播放量,0-正常，1-禁用"`
	LikeNum       int64  `gorm:"type:int(11);default:0;not null;comment:点赞数,0-正常，1-禁用"`
	CollectNum    int64  `gorm:"type:int(11);default:0;not null;comment:收藏数,0-正常，1-禁用"`
	CommentNum    int64  `gorm:"type:int(11);default:0;not null;comment: 评论数,0-正常，1-禁用"`
	VideoStatus   string `gorm:"type:varchar(255);default:1;not null;comment:视频状态,0-已发布，1-草稿，2-下架"`
	TopicId       int64  `gorm:"type:int(11);default:0;not null;comment:话题id"`
}

func (v *Video) AddVideo(db *gorm.DB) error {
	return db.Debug().Create(v).Error
}

func (v *Video) Updatavideo(db *gorm.DB, id int64) error {
	return db.Debug().Where("id = ?", id).Updates(v).Error
}

func (v *Video) DelVideo(db *gorm.DB, id int64) error {
	return db.Debug().Where("id = ?", id).Delete(v).Error
}

func (v *Video) FindVideoId(db *gorm.DB, id int64) error {
	return db.Debug().Where("videos.id = ?", id).First(v).Error
}

func VideoList(db *gorm.DB, in *videorpc.VideoListRequest) []*videorpc.VideoList {
	var list []*videorpc.VideoList
	db.Debug().Model(&Video{}).Select("videos.id as video_id,videos.video_title,videos.video_link,videos.play_num," +
		"videos.video_status,videos.cover_link,users.id as user_id,users.nick_name,users.avatar_link").
		Joins("LEFT JOIN users ON videos.user_id = users.id").
		Order("videos.play_num desc").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Find(&list)
	return list
}

func ShowVideo(db *gorm.DB, id int64, id2 int64) []*videorpc.ShowVideo {
	var lists []*videorpc.ShowVideo
	db.Debug().Model(&Video{}).Select("videos.id as video_id,videos.video_title,videos.video_link,videos.play_num,videos.video_status,videos.cover_link,"+
		"users.id as user_id,users.nick_name,users.avatar_link,topics.id as topic_id, topics.topic_name,topics.topic_info").
		Joins("LEFT JOIN users ON videos.user_id = users.id").
		Joins("LEFT JOIN topics ON videos.topic_id = topics.id").
		Where("videos.topic_id = ? OR videos.user_id = ?", id, id2).
		Order("videos.play_num desc").Limit(3).
		Find(&lists)
	return lists
}
